package com.mutual.appUserProduct;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.mutual.appUserInfo.pojo.AppUserInfoProduct;
import com.mutual.appUserInfo.service.AppUserInfoProductService;
import com.mutual.attachment.pojo.Attachment;
import com.mutual.attachment.service.AttachmentService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.commom.util.BusnessUtil;
import com.mutual.commom.util.SystemUtil;
import com.mutual.common.BaseController;
import com.mutual.common.ResultList;
import com.mutual.healthInfo.pojo.HealthInfo;
import com.mutual.healthInfo.service.HealthInfoService;
import com.mutual.product.pojo.Product;
import com.mutual.product.service.ProductService;

@Controller
@RequestMapping("/appUserProduct")
public class AppUserProductController extends BaseController{

	@Resource
	private ProductService productService;
	
	@Resource
	private AppUserInfoProductService appUserInfoProductService;
	
	
	@RequestMapping(value = "/toList")
	public String toList(Model model){
		List<Product> productList=productService.selectProductList(Maps.newHashMap());
		model.addAttribute("productList", productList);
		return "appUserProduct/appUserProductList";
	}
	
	@RequestMapping(value = "/listData")
	@ResponseBody
	public ResultList<AppUserInfoProduct> listData(@RequestParam Map<String,Object> param){
		super.setAreaCode(param);
		total=appUserInfoProductService.selectTotalCount(param);
		ResultList<AppUserInfoProduct> result=new ResultList<>();
		result.setCount(total);
		List<AppUserInfoProduct> appUserInfoProductList=appUserInfoProductService.selectAllObjPage(param);
		DateTime nowTime=DateTime.now();
		for(AppUserInfoProduct app:appUserInfoProductList){
			Period p = new Period(new DateTime(app.getApplyDate().getTime()),nowTime,PeriodType.days());  
			app.setTotalDays(p.getDays());
			app.setObservationPeriodStatus(BusnessUtil.getObservationPeriodStatus(app.getApplyDate()));
		}
		result.setData(appUserInfoProductList);
		return result;
	}
	
	
	/**
	 * 发送提醒短信
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/sendNoticeSMS")
	@ResponseBody
	public ReturnResult sendNoticeSMS(@RequestParam Map<String,Object> param) {
		ReturnResult returnResult=null;
		try {
			param.put("optionId", getCurrentUser().getId());
			param.put("optionName", getCurrentUser().getRealName());
			returnResult=appUserInfoProductService.sendNoticeSMS(param);
		} catch (Exception e) {
			returnResult=new ReturnResult();
			returnResult.setStatus(ReturnResult.STATUS_FAILED);
			returnResult.setReturnMsg("发送失败");
		}
		return returnResult;
	}
}
